package controller

import (
	"gin/common"
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
)

func DictWordSelect(ctx *gin.Context) {
	var selectDictQuery validate.SelectDictWordQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	total, resData, queryErr := service.SelectDictWord(&selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// 查询单条词库
func DictWordFind(ctx *gin.Context) {
	var selectDictQuery validate.SelectDictWordQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateFilterParam(&selectDictQuery.Param.Filter, "log")

	resData, queryErr := service.FindDictWord(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// 插入日志
func DictWordCreate(ctx *gin.Context) {
	var addDictWords validate.AddDictWordWrapper

	err := ctx.ShouldBindJSON(&addDictWords)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.CreateDictWord(addDictWords.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func DictWordUpdate(ctx *gin.Context) {
	var updateDictWord validate.UpdateDictWordWrapper

	err := ctx.ShouldBindJSON(&updateDictWord)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.UpdateDictWord(updateDictWord.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func DictWordDelete(ctx *gin.Context) {
	var selectDictQuery validate.DeleteDictWordParam

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateFilterParam(&selectDictQuery.Param, "dict_word")

	_ = service.DeleteDictWord(&selectQueryParam)

	response.Success(ctx, nil, "success")
}

func DictWordBatchUpdate(ctx *gin.Context) {
	var updateDictWord validate.BatchUpdateDictWord

	err := ctx.ShouldBindJSON(&updateDictWord)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	param := common.GenerateSelectQueryParam(updateDictWord.Param, "dict_word")
	resData, createErr := service.BatchUpdateDictWord(param, updateDictWord.ExtendCode)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func DictWordBatchCreate(ctx *gin.Context) {
	var addDictWords validate.BatchAddDictWordWrapper

	err := ctx.ShouldBindJSON(&addDictWords)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.BatchCreateDictWord(addDictWords.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}
